package DB;
import Entities.User;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUserConnector extends DBConnector {
	
	static public User getUser(String username){
		User user=null;
		String sql="{call getUser(?)}";	
		CallableStatement cs1;
		Connection conn=openConnection();
		try {
			 cs1 = conn.prepareCall(sql);
			 cs1.setString(1, username);
		     ResultSet rs=cs1.executeQuery();
	    	 rs.next();
	    	 user=new User(rs.getString("username"),rs.getString("pass"));
		     cs1.close();
		     closeConnection(conn);
		}
	     catch (SQLException e) {}
	     return user;
	}
	
	static public void addUser(User user){
		String sql="{call addUser(?,?)}";	
		CallableStatement cs1;
		Connection conn=openConnection();
		try {
			 cs1 = conn.prepareCall(sql);
			 cs1.setString(1, user.getUsername());
			 cs1.setString(2, user.getPassword());
		     cs1.execute();
		     cs1.close();
		     closeConnection(conn);
		}
	     catch (SQLException e) {e.printStackTrace();}
	}
	
	static public void deleteUser(String username){
		String sql="{call deleteUser(?)}";	
		CallableStatement cs1;
		Connection conn=openConnection();
		try {
			 cs1 = conn.prepareCall(sql);
			 cs1.setString(1, username);
		     cs1.execute();
		     cs1.close();
		     closeConnection(conn);
		}
	     catch (SQLException e) {}
	}
}